package com.grape.api.v1.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.annotation.RepeatSubmit;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.domain.MemberRechargeRecord;
import com.grape.service.IMemberRechargeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 会员充值记录表 控制器
 * @author      putao
 * @email       2628279194@qq.com
 */
@RestController
@RequestMapping("memberRechargeRecords")
@Api(tags = "会员充值记录接口")
@AllArgsConstructor
public class MemberRechargeRecordController extends BaseController<MemberRechargeRecord> {

    private final IMemberRechargeRecordService memberRechargeRecordService;


    @GetMapping
    @ApiOperation(value = "会员充值记录分页")
    @OperateMethodName(value = "会员充值记录表列表分页", url = "member:memberRechargeRecords:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<MemberRechargeRecord> pageInfo = memberRechargeRecordService.pageList(baseVo);
        List<MemberRechargeRecord> memberRechargeRecordList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(memberRechargeRecordList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @GetMapping("listPageCustom")
    @ApiOperation(value = "会员充值记录自定义分页")
    @OperateMethodName(value = "会员充值记录表列表", url = "member:memberRechargeRecords:listPageCustom")
    public R<?> pageListCustom(BaseVo baseVo) {
        Page<MemberRechargeRecord> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        memberRechargeRecordService.pageListCustom(baseVo);
        PageInfo<MemberRechargeRecord> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @RepeatSubmit
    @PutMapping
    @ApiOperation(value = "会员充值记录修改")
    @OperateMethodName(value = "会员充值记录表修改", url = "member:memberRechargeRecords:update")
    public R<?> update(@Validated @RequestBody MemberRechargeRecord memberRechargeRecord) {
        if (!Strings.isNotBlank(memberRechargeRecord.getId())) {
            return updateNotFound(memberRechargeRecord, null);
        }
        memberRechargeRecordService.updateData(memberRechargeRecord);
        return updateSuccess(null, null);
    }


    @RepeatSubmit
    @PostMapping
    @ApiOperation(value = "添加会员充值记录")
    @OperateMethodName(value = "添加会员充值记录表", url = "member:memberRechargeRecords:insert")
    public R<?> insert(@Validated @RequestBody MemberRechargeRecord memberRechargeRecord) {
        return insertSuccess(memberRechargeRecordService.insertData(memberRechargeRecord), null);
    }


    @DeleteMapping
    @ApiOperation(value = "删除会员充值记录，可批量多个id逗号拼接")
    @OperateMethodName(value = "会员充值记录表删除,批量", url = "member:memberRechargeRecords:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空") @RequestParam String ids) {
        int number = memberRechargeRecordService.deleteData(ids);
        if (number < 1) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }


    @GetMapping("detail")
    @ApiOperation(value = "会员充值记录详情")
    @OperateMethodName(value = "会员充值记录表详情", url = "member:memberRechargeRecords:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        MemberRechargeRecord memberRechargeRecord = memberRechargeRecordService.detail(id);
        if (memberRechargeRecord != null) {
            return selectSuccess(memberRechargeRecord, id);
        }
        return selectNotFound(id, null);
    }
}

